package com.tansun.easycare.rule.datamodel.entity;


import java.util.Date;
import java.util.List;

import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tansun.easycare.core.utils.DateUtils;
import com.tansun.easycare.rule.common.Constant;
import com.tansun.easycare.rule.common.entity.RuleBaseEntity;
import com.tansun.easycare.rule.common.model.Item;
import com.tansun.easycare.rule.common.service.SpringInstanceUtils;
import com.tansun.easycare.rule.datamodel.service.RuleDataparamsService;
import com.tansun.rule.utils.StringUtils;
import com.tansun.ruledata.data.entity.DeployDataparams;

/**
 * 数据集变量Entity
 *
 * @author lxs
 * @version 2017-02-24
 */
public class    RuleDataparams extends RuleBaseEntity<RuleDataparams> {

    private static final long serialVersionUID = 1L;
    private String projectcode;        // 规则库编码
    private String datasetId;        // 关联数据集编号
    private String name;        // 变量名
    private String chzn;        // 中文名
    private String type;        // 数据类型
    private String domainId;        // 码值域ID
    private String domainType;        // 取值类型（集合、线性、离散、衍生）
    private String domainStr;        // 码值域显示
    private String mathStr;        // 衍生变量公式
    private String length;        // length
    private String defaultValue;        // 默认值
    private String isCollections;        // 是否集合
    private String isDerives;        // 是否衍生变量
    private String pattern;        // 日期格式
    private String isout;        // 是否输出
    private String paramDesc;        // 描述信息
    private String domain;        // domain
    private String collection;        // collection
    private String deleteflag; //删除的标记
    private String isfuncparam;     //是否 关联函数变量
    private String formatDomainStr;
    private String formatDefaultValue;
    private String className;//扩展字段
    
    private List<Item> domainJsonStr;//扩展字段 枚举
    private String codeblockId; //扩展字段   函数代码id
    private String returnType;//扩展字段 返回类型
    private String funcImport;//扩展字段 函数格式
    private String formatcodebody;//格式化代码快
    private String codeBody;//扩展字段  函数体
    private String domainDefaultCN;//扩展字段 枚举缺省中文值
    private List<String> funcRankIdList;//函数变量的层级Id的list

    public List<String> getFuncRankIdList() {
        return funcRankIdList;
    }
    public void setFuncRankIdList(List<String> funcRankIdList) {
        this.funcRankIdList = funcRankIdList;
    }

    public RuleDataparams() {
        super();
    }

    public String getDeleteflag() {
		return deleteflag;
	}

	public void setDeleteflag(String deleteflag) {
		this.deleteflag = deleteflag;
	}

	public RuleDataparams(String id) {
        super(id);
    }

	public DeployDataparams changeClass() {
		DeployDataparams newinfo = new DeployDataparams();
		newinfo.setChzn(chzn);
		newinfo.setClassName(className);
		newinfo.setCollection(collection);
		newinfo.setDatasetId(datasetId);
		newinfo.setDefaultValue(defaultValue);
		newinfo.setDomain(domain);
		newinfo.setDomainId(domainId);
		newinfo.setDomainStr(domainStr);
		newinfo.setDomainType(domainType);
		newinfo.setId(id);
		newinfo.setIsCollections(isCollections);
		newinfo.setIsDerives(isDerives);
		newinfo.setIsout(isout);
		newinfo.setLength(length);
		newinfo.setMathStr(mathStr);
		newinfo.setName(name);
		newinfo.setParamDesc(paramDesc);
		newinfo.setPattern(pattern);
		newinfo.setProjectcode(projectcode);
		newinfo.setType(type);
		return newinfo;
	}
	
    @Length(min = 0, max = 50, message = "规则库编码长度必须介于 0 和 50 之间")
    public String getProjectcode() {
        return projectcode;
    }

    public void setProjectcode(String projectcode) {
        this.projectcode = projectcode;
    }

    @Length(min = 0, max = 50, message = "关联数据集编号长度必须介于 0 和 50 之间")
    public String getDatasetId() {
        return datasetId;
    }

    public void setDatasetId(String datasetId) {
        this.datasetId = datasetId;
    }

    @Length(min = 0, max = 50, message = "变量名长度必须介于 0 和 50 之间")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Length(min = 0, max = 50, message = "中文名长度必须介于 0 和 50 之间")
    public String getChzn() {
        return chzn;
    }

    public void setChzn(String chzn) {
        this.chzn = chzn;
    }

    @Length(min = 0, max = 10, message = "数据类型长度必须介于 0 和 10 之间")
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Length(min = 0, max = 50, message = "码值域ID长度必须介于 0 和 50 之间")
    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    @Length(min = 0, max = 20, message = "取值类型（集合、线性、离散、衍生）长度必须介于 0 和 20 之间")
    public String getDomainType() {
        return domainType;
    }

    public void setDomainType(String domainType) {
        this.domainType = domainType;
    }

    @Length(min = 0, max = 200, message = "码值域显示长度必须介于 0 和 200 之间")
    public String getDomainStr() {
        return domainStr;
    }

    public void setDomainStr(String domainStr) {
        this.domainStr = domainStr;
    }

    @Length(min = 0, max = 200, message = "衍生变量公式长度必须介于 0 和 200 之间")
    public String getMathStr() {
        return mathStr;
    }

    public void setMathStr(String mathStr) {
        this.mathStr = mathStr;
    }

    @Length(min = 0, max = 11, message = "length长度必须介于 0 和 11 之间")
    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    @Length(min = 0, max = 50, message = "默认值长度必须介于 0 和 50 之间")
    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    @Length(min = 0, max = 10, message = "是否集合长度必须介于 0 和 10 之间")
    public String getIsCollections() {
        return isCollections;
    }

    public void setIsCollections(String isCollections) {
        this.isCollections = isCollections;
    }

    @Length(min = 0, max = 10, message = "是否衍生变量长度必须介于 0 和 10 之间")
    public String getIsDerives() {
        return isDerives;
    }

    public void setIsDerives(String isDerives) {
        this.isDerives = isDerives;
    }

    @Length(min = 0, max = 20, message = "日期格式长度必须介于 0 和 20 之间")
    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    @Length(min = 0, max = 10, message = "是否输出长度必须介于 0 和 10 之间")
    public String getIsout() {
        return isout;
    }

    public void setIsout(String isout) {
        this.isout = isout;
    }

    @Length(min = 0, max = 200, message = "描述信息长度必须介于 0 和 200 之间")
    public String getParamDesc() {
        return paramDesc;
    }

    public void setParamDesc(String paramDesc) {
        this.paramDesc = paramDesc;
    }

    @Length(min = 0, max = 45, message = "domain长度必须介于 0 和 45 之间")
    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    @Length(min = 0, max = 50, message = "collection长度必须介于 0 和 50 之间")
    public String getCollection() {
        return collection;
    }

    public void setCollection(String collection) {
        this.collection = collection;
    }

    public String getFormatcodebody() {
		return formatcodebody;
	}

	public void setFormatcodebody(String formatcodebody) {
		this.formatcodebody = formatcodebody;
	}

	@JsonIgnore
    public String getFormatDomainStr() {
        if (StringUtils.isEmpty(domainStr)) {
//			if( type.equals(Constant.DATAMODEL_ATTRIBUTE_TYPE_DATE)){
//				String value1="";
//				String value2="";
//				if(domain.length()>4){
//					String[] a=domain.substring(1,(domain.length()-1)).split(",");
//					if(a.length>0) value1=a[0];
//					if(a.length>1) value2=a[1];
//				}
//				
//				if(value1!="")value1=DateUtils.formatDate(new Date(Long.parseLong(value1)),"yyyy-MM-dd");
//				if(value2!="")value2=DateUtils.formatDate(new Date(Long.parseLong(value2)),"yyyy-MM-dd");
//				return '['+value1+','+value2+']';
//			}else{
//				return domain;
//			}
            return domain;
        } else {
            return domainStr;
        }
    }

    @JsonIgnore
    public String getFormatDefaultValue() {
        if (StringUtils.isEmpty(defaultValue)) {
            return "";
        }
        if (type.equals(Constant.DATAMODEL_ATTRIBUTE_TYPE_DATE)) {
            if (StringUtils.isNumeric(this.defaultValue)) {
                return DateUtils.formatDate(new Date(Long.parseLong(this.defaultValue)), "yyyy-MM-dd");
            } else {
                return this.defaultValue;
            }

        }
        if (!StringUtils.isEmpty(domain) && type.equals(Constant.DATAMODEL_ATTRIBUTE_TYPE_STRING)) {
            RuleDataparamsService ruleDataparamsService = (RuleDataparamsService) SpringInstanceUtils.instance.getSpringBean("ruleDataparamsService");
            RuleDataDomain dataDomain = new RuleDataDomain();
            dataDomain.setDomainId(id);
            dataDomain.setDomainValue(this.defaultValue);
            List<RuleDataDomain> list = ruleDataparamsService.findDataDomainList(dataDomain);
            if (list != null && list.size() > 0) {
                return list.get(0).getName();
            }
            return null;
        }
        return defaultValue;
    }

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}
	
	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public String getFuncImport() {
		return funcImport;
	}

	public void setFuncImport(String funcImport) {
		this.funcImport = funcImport;
	}

	public String getCodeBody() {
		return codeBody;
	}

	public void setCodeBody(String codeBody) {
		this.codeBody = codeBody;
	}



	public String getIsfuncparam() {
		return isfuncparam;
	}

	public void setIsfuncparam(String isfuncparam) {
		this.isfuncparam = isfuncparam;
	}

	public String getCodeblockId() {
		return codeblockId;
	}

	public void setCodeblockId(String codeblockId) {
		this.codeblockId = codeblockId;
	}

	public List<Item> getDomainJsonStr() {
		return domainJsonStr;
	}

	public void setDomainJsonStr(List<Item> domainJsonStr) {
		this.domainJsonStr = domainJsonStr;
	}

	//	public String getSql() {
//		return "INSERT INTO rule_dataparams(id,projectcode,dataset_id,name,chzn,type,domain_id,domain_type,domain_str,math_str,length,default_value,is_collections,is_derives,pattern,isout,param_desc,create_date,domain,collection,data_version)"
//				+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
//	}
//	public Object[] getArgs(String timeStamp) {
//		return new Object[] {this.id,this.projectcode,this.datasetId,this.name,this.chzn,this.type, this.domainId,this.domainType,this.domainStr,this.mathStr,this.length,this.defaultValue,this.isCollections,this.isDerives,this.pattern,this.isout,this.paramDesc,new Date(),this.domain,this.collection,timeStamp};
//	}
	public String getSql() {
		return "INSERT INTO rule_dataparams(id,projectcode,dataset_id,name,chzn,type,domain_id,domain_type,domain_str,math_str,length,default_value,is_collections,is_derives,pattern,isout,param_desc,create_date,domain,collection,isfuncparam,data_version) values ";
	}
	public String getArgs(String timeStamp) {
		return "('"+this.id+"','"
				+isEmpty(this.projectcode)+"','"
				+isEmpty(this.datasetId)+"','"
				+isEmpty(this.name)+"','"
				+isEmpty(this.chzn)+"','"
				+isEmpty(this.type)+"','"
				+isEmpty(this.domainId)+"','"
				+isEmpty(this.domainType)+"','"
				+isEmpty(this.domainStr)+"','"
				+isEmpty(this.mathStr)+"','"
				+(isEmpty(this.length)==""?"0":this.length)+"','"
				+isEmpty(this.defaultValue)+"','"
				+isEmpty(this.isCollections)+"','"
				+isEmpty(this.isDerives)+"','"
				+isEmpty(this.pattern)+"','"
				+isEmpty(this.isout)+"','"
				+isEmpty(this.paramDesc)+"',@currDate,'"
				+isEmpty(this.domain)+"','"
				+isEmpty(this.collection)+"','"
				+isEmpty(this.isfuncparam)+"','"
				+timeStamp+"')";
	}
	public String getArgs(String timeStamp,String currDate) {
		return "('"+this.id+"','"
				+isEmpty(this.projectcode)+"','"
				+isEmpty(this.datasetId)+"','"
				+isEmpty(this.name)+"','"
				+isEmpty(this.chzn)+"','"
				+isEmpty(this.type)+"','"
				+isEmpty(this.domainId)+"','"
				+isEmpty(this.domainType)+"','"
				+isEmpty(this.domainStr)+"','"
				+isEmpty(this.mathStr)+"','"
				+(isEmpty(this.length)==""?"0":this.length)+"','"
				+isEmpty(this.defaultValue)+"','"
				+isEmpty(this.isCollections)+"','"
				+isEmpty(this.isDerives)+"','"
				+isEmpty(this.pattern)+"','"
				+isEmpty(this.isout)+"','"
				+isEmpty(this.paramDesc)+"','"+currDate+"','"
				+isEmpty(this.domain)+"','"
				+isEmpty(this.collection)+"','"
				+isEmpty(this.isfuncparam)+"','"
				+timeStamp+"')";
	}

	public String getDomainDefaultCN() {
		return domainDefaultCN;
	}

	public void setDomainDefaultCN(String domainDefaultCN) {
		this.domainDefaultCN = domainDefaultCN;
	}
	
	
}